/**
 * 系统资源模块模型监控
 * 
 * @author yangyibao
 */

//CPU监控
var cpuStatChart = echarts.init(document.getElementById('sysCpu'));

//CPU数据设置
var cpuOption = {
		tooltip : {formatter: "{a} <br/>{b} : {c}%"},
		series: [{name: '系统CPU',radius: '100%',axisLine: {lineStyle: {width: 12}},type: 'gauge',detail: {formatter:'{value}%'},data: [{value:0,name: 'CPU利用率'}]}]
    }

//加载CPU数据  两秒钟更新一次
cpuStatChart.setOption(cpuOption);

var CputimeTicket;
clearInterval(CputimeTicket);
CputimeTicket = setInterval(function () {
	cpu();
},2000);

//内存监控 
var memStatChart = echarts.init(document.getElementById('sysMem'));
//内存数据设置
var memOption = {
		tooltip : {formatter: "{a} <br/>{b} : {c}%"},
		series: [{name: '系统内存',radius: '100%',axisLine: {lineStyle: {width: 12}},type: 'gauge',detail: {formatter:'{value}%'},data: [{value: 0, name: 'Mem占用率'}]}]
 }
//内存数据  一分钟更新一次
memStatChart.setOption(memOption);
var MemtimeTicket;
clearInterval(MemtimeTicket);
MemtimeTicket = setInterval(function(){
	mem();
},60000);


//磁盘监控
var diskChart = echarts.init(document.getElementById('sysDisk'));
//磁盘数据设置
var diskOption = {
      tooltip: {trigger: 'axis'},
      legend: {data: ['磁盘信息(G)']},
      calculable: true,
      xAxis: [{type: 'category',data: []}],
      yAxis: [{type: 'value',splitArea: {show: true}}], 
      grid: {left: '3%',right: '4%',bottom: '3%',containLabel: true},
      series: [{name: '磁盘使用率(%)',barWidth : 25,radius: '100%',type: 'bar',data: []}]
  }
//加载磁盘数据 一个小时更新一次
diskChart.setOption(diskOption);
var DisktimeTicket;
clearInterval(DisktimeTicket);
DisktimeTicket = setInterval(function(){
	disk();
},3600000);


//jvm监控
var jvmStatChart =echarts.init(document.getElementById('sysJvm')); 
var date = [];
var data = [];
function addData(shift) {
	var now = new Date()
    date.push(now.getHours()+":"+now.getMinutes()+":"+now.getSeconds());
    $.get("../system/sysJvmMonitor",function(value){
    	data.push(value);  
    	if (shift) {
            data.shift();
        }
    }); 
    if (shift) {
        date.shift();
    }
}
for (var i = 1; i < 5; i++) {
    addData();
}
var jvmOption = {
    xAxis: {
        type: 'category',
        boundaryGap: false,
        data: date
    },
    yAxis: {
        boundaryGap: [0, '50%'],
        type: 'value',
        max:'512',
        splitNumber:10
    },
    tooltip:{
    	trigger:'axis',
    	formatter:function(params){
    		params = params[0];
    		var date = new Date(params.name);
    		return params.name+'  /  '+params.data+'M';
    	},
    },
    series: [
        {
            name:'成交',
            type:'line',
            smooth:true,
            symbol: 'none',
            stack: 'a',
            areaStyle: {
                normal: {}
            },
            data: data
        }
    ]
};
//加载数据
jvmStatChart.setOption(jvmOption);
var jvmtimeTicket;
clearInterval(jvmtimeTicket);
jvmtimeTicket = setInterval(function () {
    addData(true);
    jvmStatChart.setOption({
        xAxis: {
            data: date
        },
        series: [{
            name:'成交',
            data: data
        }]
    });
}, 1000);

//封装cpu的方法
function cpu(){
	$.get("../system/sysCpuMonitor",function(data){
		cpuOption.series[0].data[0].value = data;
		cpuStatChart.setOption(cpuOption);
	});
}
//封装mem内存的方法
function mem(){
	$.get("../system/sysMemMonitor",function(data){
		memOption.series[0].data[0].value=data;
		memStatChart.setOption(memOption);
	});
}
//封装磁盘的方法
function disk(){
	$.get("../system/sysdiscMonitor",function(data){
		diskOption.series[0].data = data.size;
		diskOption.xAxis[0].data = data.fsNames;
		diskChart.setOption(diskOption);
	});
}
$(function(){
	cpu();
	mem();
	disk();
});